home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / kgdb-4.5 / ds3100.md / gdb / mips-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-22  |  26.2 KB  |  854 lines

  1. /* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
  2.    Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
  4.    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "defs.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25. #include "symtab.h"
  26. #include "value.h"
  27. #include "gdbcmd.h"
  28. #include "language.h"
  29.  
  30. #ifdef USG
  31. #include <sys/types.h>
  32. #endif
  33.  
  34. #include <sys/param.h>
  35. #include <sys/dir.h>
  36. #include <signal.h>
  37. #include <sys/ioctl.h>
  38.  
  39. #include <setjmp.h>
  40.  
  41. #include "gdbcore.h"
  42. #include "symfile.h"
  43. #include "objfiles.h"
  44.  
  45. #ifndef    MIPSMAGIC
  46. #ifdef MIPSEL
  47. #define MIPSMAGIC    MIPSELMAGIC
  48. #else
  49. #define MIPSMAGIC    MIPSEBMAGIC
  50. #endif
  51. #endif
  52.  
  53. #define VM_MIN_ADDRESS (unsigned)0x400000
  54.  
  55. #include <sys/user.h>        /* After a.out.h  */
  56. #include <sys/file.h>
  57. #include <sys/stat.h>
  58.  
  59. #ifdef KGDB
  60. #include <kernel/ds3100.md/machAsmDefs.h>
  61. #endif
  62.  
  63.  
  64. #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
  65. #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
  66. #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
  67. #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
  68. #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
  69. #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
  70. #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
  71. #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
  72. #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
  73. #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
  74. #define _PROC_MAGIC_ 0x0F0F0F0F
  75. #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
  76. #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
  77.  
  78. struct linked_proc_info
  79. {
  80.   struct mips_extra_func_info info;
  81.   struct linked_proc_info *next;
  82. } * linked_proc_desc_table = NULL;
  83.  
  84.  
  85. #define READ_FRAME_REG(fi, regno) read_next_frame_reg((fi)->next, regno)
  86.  
  87. static int
  88. read_next_frame_reg(fi, regno)
  89.      FRAME fi;
  90.      int regno;
  91. {
  92. #define SIGFRAME_BASE   sizeof(struct sigcontext)
  93. #define SIGFRAME_PC_OFF (-SIGFRAME_BASE+ 2*sizeof(int))
  94. #define SIGFRAME_SP_OFF (-SIGFRAME_BASE+32*sizeof(int))
  95. #define SIGFRAME_RA_OFF (-SIGFRAME_BASE+34*sizeof(int))
  96.   for (; fi; fi = fi->next)
  97.       if (in_sigtramp(fi->pc, 0)) {
  98.       /* No idea if this code works. --PB. */
  99.       int offset;
  100.       if (regno == PC_REGNUM) offset = SIGFRAME_PC_OFF;
  101.       else if (regno == RA_REGNUM) offset = SIGFRAME_RA_OFF;
  102.       else if (regno == SP_REGNUM) offset = SIGFRAME_SP_OFF;
  103.       else return 0;
  104.       return read_memory_integer(fi->frame + offset, 4);
  105.       }
  106.       else if (regno == SP_REGNUM) return fi->frame;
  107.       else if (fi->saved_regs->regs[regno])
  108.     return read_memory_integer(fi->saved_regs->regs[regno], 4);
  109.   return read_register(regno);
  110. }
  111.  
  112. int
  113. mips_frame_saved_pc(frame)
  114.      FRAME frame;
  115. {
  116.   mips_extra_func_info_t proc_desc = (mips_extra_func_info_t)frame->proc_desc;
  117.   int pcreg = proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM;
  118.  
  119.   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
  120.       return read_memory_integer(frame->frame - 4, 4);
  121. #ifdef KGDB
  122.   /*
  123.    * Things are a little bit tricky here. Leaf routines may have a zero-size
  124.    * frame. Unfortunately, we may get an exception or interrupt while
  125.    * we are in a leaf routine, so that additional frames are pushed on the
  126.    * stack. The return address of the leaf frame is not on the stack (there
  127.    * is no room for it). Instead, the routine Mach_KernGenException pushes
  128.    * all registers onto the stack, and we can find the return address there.
  129.    * The current frame is the zero-sized one, and we have to go back a
  130.    * couple to get the frame address we want.
  131.    */
  132.   if (proc_desc && (PROC_FRAME_OFFSET(proc_desc) == 0)) {
  133.       int    addr;
  134.       if (frame->next == NULL) {
  135.       return read_register(pcreg);
  136.       }
  137.       if (frame->next == NULL) {
  138.       return 0;
  139.       }
  140.       if (frame->next->next == NULL) {
  141.       return 0;
  142.       }
  143.       addr = frame->next->next->frame + 
  144.       STAND_FRAME_SIZE + 8 + SAVED_REG_SIZE - 4;
  145.       return read_memory_integer(addr, 4);
  146.   }
  147. #endif
  148.  
  149.   return read_next_frame_reg(frame, pcreg);
  150. }
  151.  
  152. static struct mips_extra_func_info temp_proc_desc;
  153. static struct frame_saved_regs temp_saved_regs;
  154.  
  155. static CORE_ADDR
  156. heuristic_proc_start(pc)
  157.     CORE_ADDR pc;
  158. {
  159.  
  160.     CORE_ADDR start_pc = pc;
  161.     CORE_ADDR fence = start_pc - 200;
  162.     if (fence < VM_MIN_ADDRESS) fence = VM_MIN_ADDRESS;
  163.     /* search back for previous return */
  164.     for (start_pc -= 4; ; start_pc -= 4)
  165.     if (start_pc < fence) return 0; 
  166.     else if (ABOUT_TO_RETURN(start_pc))
  167.         break;
  168.  
  169.     start_pc += 8; /* skip return, and its delay slot */
  170. #if 0
  171.     /* skip nops (usually 1) 0 - is this */
  172.     while (start_pc < pc && read_memory_integer (start_pc, 4) == 0)
  173.     start_pc += 4;
  174. #endif
  175.     return start_pc;
  176. }
  177.  
  178. static mips_extra_func_info_t
  179. heuristic_proc_desc(start_pc, limit_pc, next_frame)
  180.     CORE_ADDR start_pc, limit_pc;
  181.     FRAME next_frame;
  182. {
  183.     CORE_ADDR sp = next_frame ? next_frame->frame : read_register (SP_REGNUM);
  184.     CORE_ADDR cur_pc;
  185.     int frame_size;
  186.     int has_frame_reg = 0;
  187.     int reg30; /* Value of $r30. Used by gcc for frame-pointer */
  188.     unsigned long reg_mask = 0;
  189.  
  190.     if (start_pc == 0) return NULL;
  191.     bzero(&temp_proc_desc, sizeof(temp_proc_desc));
  192.     bzero(&temp_saved_regs, sizeof(struct frame_saved_regs));
  193.     if (start_pc + 200 < limit_pc) limit_pc = start_pc + 200;
  194.   restart:
  195.     frame_size = 0;
  196.     for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4) {
  197.     unsigned long word;
  198.     int status;
  199.  
  200.     status = read_memory_nobpt (cur_pc, &word, 4); 
  201.     if (status) memory_error (status, cur_pc); 
  202.     SWAP_TARGET_AND_HOST (&word, sizeof (word));
  203.     if ((word & 0xFFFF0000) == 0x27bd0000) /* addiu $sp,$sp,-i */
  204.         frame_size += (-word) & 0xFFFF;
  205.     else if ((word & 0xFFFF0000) == 0x23bd0000) /* addu $sp,$sp,-i */
  206.         frame_size += (-word) & 0xFFFF;
  207.     else if ((word & 0xFFE00000) == 0xafa00000) { /* sw reg,offset($sp) */
  208.         int reg = (word & 0x001F0000) >> 16;
  209.         reg_mask |= 1 << reg;
  210.         temp_saved_regs.regs[reg] = sp + (short)word;
  211.     }
  212.     else if ((word & 0xFFFF0000) == 0x27be0000) { /* addiu $30,$sp,size */
  213.         if ((unsigned short)word != frame_size)
  214.         reg30 = sp + (unsigned short)word;
  215.         else if (!has_frame_reg) {
  216.         int alloca_adjust;
  217.         has_frame_reg = 1;
  218.         reg30 = read_next_frame_reg(next_frame, 30);
  219.         alloca_adjust = reg30 - (sp + (unsigned short)word);
  220.         if (alloca_adjust > 0) {
  221.             /* FP > SP + frame_size. This may be because
  222.             /* of an alloca or somethings similar.
  223.              * Fix sp to "pre-alloca" value, and try again.
  224.              */
  225.             sp += alloca_adjust;
  226.             goto restart;
  227.         }
  228.         }
  229.     }
  230.     else if ((word & 0xFFE00000) == 0xafc00000) { /* sw reg,offset($30) */
  231.         int reg = (word & 0x001F0000) >> 16;
  232.         reg_mask |= 1 << reg;
  233.         temp_saved_regs.regs[reg] = reg30 + (short)word;
  234.     }
  235.     }
  236.     if (has_frame_reg) {
  237.     PROC_FRAME_REG(&temp_proc_desc) = 30;
  238.     PROC_FRAME_OFFSET(&temp_proc_desc) = 0;
  239.     }
  240.     else {
  241.     PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
  242.     PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
  243.     }
  244.     PROC_REG_MASK(&temp_proc_desc) = reg_mask;
  245.     PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
  246.     return &temp_proc_desc;
  247. }
  248.  
  249. static mips_extra_func_info_t
  250. find_proc_desc(pc, next_frame)
  251.     CORE_ADDR pc;
  252.     FRAME next_frame;
  253. {
  254.   mips_extra_func_info_t proc_desc;
  255.   struct block *b = block_for_pc(pc);
  256.   struct symbol *sym =
  257.       b ? lookup_symbol(".gdbinfo.", b, LABEL_NAMESPACE, 0, NULL) : NULL;
  258.  
  259.   if (sym)
  260.     {
  261.     /* IF this is the topmost frame AND
  262.      * (this proc does not have debugging information OR
  263.      * the PC is in the procedure prologue)
  264.      * THEN create a "heuristic" proc_desc (by analyzing
  265.      * the actual code) to replace the "official" proc_desc.
  266.      */
  267.     proc_desc = (mips_extra_func_info_t)SYMBOL_VALUE(sym);
  268.     if (next_frame == NULL) {
  269.         struct symtab_and_line val;
  270.         struct symbol *proc_symbol =
  271.         PROC_DESC_IS_DUMMY(proc_desc) ? 0 : PROC_SYMBOL(proc_desc);
  272.  
  273.         if (proc_symbol) {
  274.         val = find_pc_line (BLOCK_START
  275.                     (SYMBOL_BLOCK_VALUE(proc_symbol)),
  276.                     0);
  277.         val.pc = val.end ? val.end : pc;
  278.         }
  279.         if (!proc_symbol || pc < val.pc) {
  280.         mips_extra_func_info_t found_heuristic =
  281.             heuristic_proc_desc(PROC_LOW_ADDR(proc_desc),
  282.                     pc, next_frame);
  283.         if (found_heuristic) proc_desc = found_heuristic;
  284.         }
  285.     }
  286.     }
  287.   else
  288.     {
  289.       /* Is linked_proc_desc_table really necessary?  It only seems to be used
  290.      by procedure call dummys.  However, the procedures being called ought
  291.      to have their own proc_descs, and even if they don't,
  292.      heuristic_proc_desc knows how to create them! */
  293.  
  294.       register struct linked_proc_info *link;
  295.       for (link = linked_proc_desc_table; link; link = link->next)
  296.       if (PROC_LOW_ADDR(&link->info) <= pc
  297.           && PROC_HIGH_ADDR(&link->info) > pc)
  298.           return &link->info;
  299.       proc_desc =
  300.       heuristic_proc_desc(heuristic_proc_start(pc), pc, next_frame);
  301.     }
  302.   return proc_desc;
  303. }
  304.  
  305. mips_extra_func_info_t cached_proc_desc;
  306.  
  307. FRAME_ADDR
  308. mips_frame_chain(frame)
  309.     FRAME frame;
  310. {
  311.     mips_extra_func_info_t proc_desc;
  312.     CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
  313. #ifndef KGDB
  314.     if (saved_pc == 0 || inside_entry_file (saved_pc))
  315.       return 0;
  316. #else
  317.     if (saved_pc == 0)
  318.       return 0;
  319. #endif
  320.     proc_desc = find_proc_desc(saved_pc, frame);
  321.     if (!proc_desc)
  322.       return 0;
  323.     cached_proc_desc = proc_desc;
  324.     return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
  325.       + PROC_FRAME_OFFSET(proc_desc);
  326. }
  327.  
  328. void
  329. init_extra_frame_info(fci)
  330.      struct frame_info *fci;
  331. {
  332.   extern struct obstack frame_cache_obstack;
  333.   /* Use proc_desc calculated in frame_chain */
  334.   mips_extra_func_info_t proc_desc = fci->next ? cached_proc_desc :
  335.       find_proc_desc(fci->pc, fci->next);
  336.  
  337.   fci->saved_regs = (struct frame_saved_regs*)
  338.     obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
  339.   bzero(fci->saved_regs, sizeof(struct frame_saved_regs));
  340.   fci->proc_desc =
  341.       proc_desc == &temp_proc_desc ? 0 : proc_desc;
  342.   if (proc_desc)
  343.     {
  344.       int ireg;
  345.       CORE_ADDR reg_position;
  346.       unsigned long mask;
  347.       /* r0 bit means kernel trap */
  348.       int kernel_trap = PROC_REG_MASK(proc_desc) & 1;
  349.  
  350.       /* Fixup frame-pointer - only needed for top frame */
  351.       /* This may not be quite right, if procedure has a real frame register */
  352.       if (fci->pc == PROC_LOW_ADDR(proc_desc))
  353.       fci->frame = read_register (SP_REGNUM);
  354.       else
  355.       fci->frame = READ_FRAME_REG(fci, PROC_FRAME_REG(proc_desc))
  356.           + PROC_FRAME_OFFSET(proc_desc);
  357.  
  358.       if (proc_desc == &temp_proc_desc)
  359.       *fci->saved_regs = temp_saved_regs;
  360.       else
  361.       {
  362.       /* find which general-purpose registers were saved */
  363.       reg_position = fci->frame + PROC_REG_OFFSET(proc_desc);
  364.       mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK(proc_desc);
  365.       for (ireg= 31; mask; --ireg, mask <<= 1)
  366.           if (mask & 0x80000000)
  367.           {
  368.           fci->saved_regs->regs[ireg] = reg_position;
  369.           reg_position -= 4;
  370.           }
  371.       /* find which floating-point registers were saved */
  372.       reg_position = fci->frame + PROC_FREG_OFFSET(proc_desc);
  373.       /* The freg_offset points to where the first *double* register is saved.
  374.        * So skip to the high-order word. */
  375.       reg_position += 4;
  376.       mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK(proc_desc);
  377.       for (ireg = 31; mask; --ireg, mask <<= 1)
  378.           if (mask & 0x80000000)
  379.           {
  380.           fci->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
  381.           reg_position -= 4;
  382.           }
  383.       }
  384.  
  385.       /* hack: if argument regs are saved, guess these contain args */
  386.       if ((PROC_REG_MASK(proc_desc) & 0xF0) == 0) fci->num_args = -1;
  387.       else if ((PROC_REG_MASK(proc_desc) & 0x80) == 0) fci->num_args = 4;
  388.       else if ((PROC_REG_MASK(proc_desc) & 0x40) == 0) fci->num_args = 3;
  389.       else if ((PROC_REG_MASK(proc_desc) & 0x20) == 0) fci->num_args = 2;
  390.       else if ((PROC_REG_MASK(proc_desc) & 0x10) == 0) fci->num_args = 1;
  391.  
  392.       fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
  393.     }
  394. }
  395.  
  396.  
  397. CORE_ADDR
  398. mips_push_arguments(nargs, args, sp, struct_return, struct_addr)
  399.   int nargs;
  400.   value *args;
  401.   CORE_ADDR sp;
  402.   int struct_return;
  403.   CORE_ADDR struct_addr;
  404. {
  405.   CORE_ADDR buf;
  406.   register i;
  407.   int accumulate_size = struct_return ? 4 : 0;
  408.   struct mips_arg { char *contents; int len; int offset; };
  409.   struct mips_arg *mips_args =
  410.       (struct mips_arg*)alloca(nargs * sizeof(struct mips_arg));
  411.   register struct mips_arg *m_arg;
  412.   for (i = 0, m_arg = mips_args; i < nargs; i++, m_arg++) {
  413.     extern value value_arg_coerce();
  414.     value arg = value_arg_coerce (args[i]);
  415.     m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
  416.     /* This entire mips-specific routine is because doubles must be aligned
  417.      * on 8-byte boundaries. It still isn't quite right, because MIPS decided
  418.      * to align 'struct {int a, b}' on 4-byte boundaries (even though this
  419.      * breaks their varargs implementation...). A correct solution
  420.      * requires an simulation of gcc's 'alignof' (and use of 'alignof'
  421.      * in stdarg.h/varargs.h).
  422.      */
  423.     if (m_arg->len > 4) accumulate_size = (accumulate_size + 7) & -8;
  424.     m_arg->offset = accumulate_size;
  425.     accumulate_size = (accumulate_size + m_arg->len + 3) & -4;
  426.     m_arg->contents = VALUE_CONTENTS(arg);
  427.   }
  428.   accumulate_size = (accumulate_size + 7) & (-8);
  429.   if (accumulate_size < 16) accumulate_size = 16; 
  430.   sp -= accumulate_size;
  431.   for (i = nargs; m_arg--, --i >= 0; )
  432.     write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
  433.   if (struct_return) {
  434.     buf = struct_addr;
  435.     write_memory(sp, &buf, sizeof(CORE_ADDR));
  436. }
  437.   return sp;
  438. }
  439.  
  440. /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
  441. #define MASK(i,j) ((1 << (j)+1)-1 ^ (1 << (i))-1)
  442.  
  443. void
  444. mips_push_dummy_frame()
  445. {
  446.   int ireg;
  447.   struct linked_proc_info *link = (struct linked_proc_info*)
  448.       xmalloc(sizeof(struct linked_proc_info));
  449.   mips_extra_func_info_t proc_desc = &link->info;
  450.   CORE_ADDR sp = read_register (SP_REGNUM);
  451.   CORE_ADDR save_address;
  452.   REGISTER_TYPE buffer;
  453.   link->next = linked_proc_desc_table;
  454.   linked_proc_desc_table = link;
  455. #define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */
  456. #define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<31)
  457. #define GEN_REG_SAVE_COUNT 22
  458. #define FLOAT_REG_SAVE_MASK MASK(0,19)
  459. #define FLOAT_REG_SAVE_COUNT 20
  460. #define SPECIAL_REG_SAVE_COUNT 4
  461.   /*
  462.    * The registers we must save are all those not preserved across
  463.    * procedure calls. Dest_Reg (see tm-mips.h) must also be saved.
  464.    * In addition, we must save the PC, and PUSH_FP_REGNUM.
  465.    * (Ideally, we should also save MDLO/-HI and FP Control/Status reg.)
  466.    *
  467.    * Dummy frame layout:
  468.    *  (high memory)
  469.    *     Saved PC
  470.    *    Saved MMHI, MMLO, FPC_CSR
  471.    *    Saved R31
  472.    *    Saved R28
  473.    *    ...
  474.    *    Saved R1
  475.    *    Saved D18 (i.e. F19, F18)
  476.    *    ...
  477.    *    Saved D0 (i.e. F1, F0)
  478.    *    CALL_DUMMY (subroutine stub; see m-mips.h)
  479.    *    Parameter build area (not yet implemented)
  480.    *  (low memory)
  481.    */
  482.   PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
  483.   PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK;
  484.   PROC_REG_OFFSET(proc_desc) = /* offset of (Saved R31) from FP */
  485.       -sizeof(long) - 4 * SPECIAL_REG_SAVE_COUNT;
  486.   PROC_FREG_OFFSET(proc_desc) = /* offset of (Saved D18) from FP */
  487.       -sizeof(double) - 4 * (SPECIAL_REG_SAVE_COUNT + GEN_REG_SAVE_COUNT);
  488.   /* save general registers */
  489.   save_address = sp + PROC_REG_OFFSET(proc_desc);
  490.   for (ireg = 32; --ireg >= 0; )
  491.     if (PROC_REG_MASK(proc_desc) & (1 << ireg))
  492.       {
  493.     buffer = read_register (ireg);
  494.     write_memory (save_address, &buffer, sizeof(REGISTER_TYPE));
  495.     save_address -= 4;
  496.       }
  497.   /* save floating-points registers */
  498.   save_address = sp + PROC_FREG_OFFSET(proc_desc);
  499.   for (ireg = 32; --ireg >= 0; )
  500.     if (PROC_FREG_MASK(proc_desc) & (1 << ireg))
  501.       {
  502.     buffer = read_register (ireg + FP0_REGNUM);
  503.     write_memory (save_address, &buffer, 4);
  504.     save_address -= 4;
  505.       }
  506.   write_register (PUSH_FP_REGNUM, sp);
  507.   PROC_FRAME_REG(proc_desc) = PUSH_FP_REGNUM;
  508.   PROC_FRAME_OFFSET(proc_desc) = 0;
  509.   buffer = read_register (PC_REGNUM);
  510.   write_memory (sp - 4, &buffer, sizeof(REGISTER_TYPE));
  511.   buffer = read_register (HI_REGNUM);
  512.   write_memory (sp - 8, &buffer, sizeof(REGISTER_TYPE));
  513.   buffer = read_register (LO_REGNUM);
  514.   write_memory (sp - 12, &buffer, sizeof(REGISTER_TYPE));
  515.   buffer = read_register (FCRCS_REGNUM);
  516.   write_memory (sp - 16, &buffer, sizeof(REGISTER_TYPE));
  517.   sp -= 4 * (GEN_REG_SAVE_COUNT+FLOAT_REG_SAVE_COUNT+SPECIAL_REG_SAVE_COUNT);
  518.   write_register (SP_REGNUM, sp);
  519.   PROC_LOW_ADDR(proc_desc) = sp - CALL_DUMMY_SIZE + CALL_DUMMY_START_OFFSET;
  520.   PROC_HIGH_ADDR(proc_desc) = sp;
  521.   SET_PROC_DESC_IS_DUMMY(proc_desc);
  522.   PROC_PC_REG(proc_desc) = RA_REGNUM;
  523. }
  524.  
  525. void
  526. mips_pop_frame()
  527. { register int regnum;
  528.   FRAME frame = get_current_frame ();
  529.   CORE_ADDR new_sp = frame->frame;
  530.   mips_extra_func_info_t proc_desc = (mips_extra_func_info_t)frame->proc_desc;
  531.   if (PROC_DESC_IS_DUMMY(proc_desc))
  532.     {
  533.       struct linked_proc_info **ptr = &linked_proc_desc_table;;
  534.       for (; &ptr[0]->info != proc_desc; ptr = &ptr[0]->next )
  535.       if (ptr[0] == NULL) abort();
  536.       *ptr = ptr[0]->next;
  537.       free (ptr[0]);
  538.       write_register (HI_REGNUM, read_memory_integer(new_sp - 8, 4));
  539.       write_register (LO_REGNUM, read_memory_integer(new_sp - 12, 4));
  540.       write_register (FCRCS_REGNUM, read_memory_integer(new_sp - 16, 4));
  541.     }
  542.   write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
  543.   if (frame->proc_desc) {
  544.     for (regnum = 32; --regnum >= 0; )
  545.       if (PROC_REG_MASK(proc_desc) & (1 << regnum))
  546.     write_register (regnum,
  547.           read_memory_integer (frame->saved_regs->regs[regnum], 4));
  548.     for (regnum = 32; --regnum >= 0; )
  549.       if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
  550.     write_register (regnum + FP0_REGNUM,
  551.           read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 4));
  552.   }
  553.   write_register (SP_REGNUM, new_sp);
  554.   flush_cached_frames ();
  555.   set_current_frame (create_new_frame (new_sp, read_pc ()));
  556. }
  557.  
  558. static void
  559. mips_print_register(regnum, all)
  560.      int regnum, all;
  561. {
  562.       unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
  563.       REGISTER_TYPE val;
  564.  
  565.       /* Get the data in raw format.  */
  566.       if (read_relative_register_raw_bytes (regnum, raw_buffer))
  567.     {
  568.       printf_filtered ("%s: [Invalid]", reg_names[regnum]);
  569.       return;
  570.     }
  571.       
  572.       /* If an even floating pointer register, also print as double. */
  573.       if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM+32
  574.       && !((regnum-FP0_REGNUM) & 1)) {
  575.       read_relative_register_raw_bytes (regnum+1, raw_buffer+4);
  576.       printf_filtered ("(d%d: ", regnum-FP0_REGNUM);
  577.       val_print (builtin_type_double, raw_buffer, 0,
  578.              stdout, 0, 1, 0, Val_pretty_default);
  579.       printf_filtered ("); ");
  580.       }
  581.       fputs_filtered (reg_names[regnum], stdout);
  582. #ifndef NUMERIC_REG_NAMES
  583.       if (regnum < 32)
  584.       printf_filtered ("(r%d): ", regnum);
  585.       else
  586. #endif
  587.       printf_filtered (": ");
  588.  
  589.       /* If virtual format is floating, print it that way.  */
  590.       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT
  591.       && ! INVALID_FLOAT (raw_buffer, REGISTER_VIRTUAL_SIZE(regnum))) {
  592.       val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0,
  593.              stdout, 0, 1, 0, Val_pretty_default);
  594.       }
  595.       /* Else print as integer in hex.  */
  596.       else
  597.     {
  598.       long val;
  599.  
  600.       bcopy (raw_buffer, &val, sizeof (long));
  601.       SWAP_TARGET_AND_HOST ((char *)&val, sizeof (long));
  602.       if (val == 0)
  603.         printf_filtered ("0");
  604.       else if (all)
  605.         printf_filtered (local_hex_format(), val);
  606.       else
  607.         printf_filtered ("%s=%d", local_hex_string(val), val);
  608.     }
  609. }
  610.  
  611. /* Replacement for generic do_registers_info.  */
  612. void
  613. mips_do_registers_info (regnum, fpregs)
  614.      int regnum;
  615.      int fpregs;
  616. {
  617.   if (regnum != -1) {
  618.       mips_print_register (regnum, 0);
  619.       printf_filtered ("\n");
  620.   }
  621.   else {
  622.       for (regnum = 0; regnum < NUM_REGS; ) {
  623.       if ((!fpregs) && regnum >= FP0_REGNUM && regnum <= FCRIR_REGNUM) {
  624.         regnum++;
  625.         continue;
  626.       }
  627.       mips_print_register (regnum, 1);
  628.       regnum++;
  629.       if ((regnum & 3) == 0 || regnum == NUM_REGS)
  630.           printf_filtered (";\n");
  631.       else
  632.           printf_filtered ("; ");
  633.       }
  634.   }
  635. }
  636. /* Return number of args passed to a frame. described by FIP.
  637.    Can return -1, meaning no way to tell.  */
  638.  
  639. int
  640. mips_frame_num_args(fip)
  641.     FRAME fip;
  642. {
  643. #if 0
  644.     struct chain_info_t *p;
  645.  
  646.     p = mips_find_cached_frame(FRAME_FP(fip));
  647.     if (p->valid)
  648.         return p->the_info.numargs;
  649. #endif
  650.     return -1;
  651. }
  652.  
  653.  
  654. /* Bad floats: Returns 0 if P points to a valid IEEE floating point number,
  655.    1 if P points to a denormalized number or a NaN. LEN says whether this is
  656.    a single-precision or double-precision float */
  657. #define SINGLE_EXP_BITS  8
  658. #define DOUBLE_EXP_BITS 11
  659. int
  660. isa_NAN(p, len)
  661.      int *p, len;
  662. {
  663.   int exponent;
  664.   if (len == 4)
  665.     {
  666.       exponent = *p;
  667.       exponent = exponent << 1 >> (32 - SINGLE_EXP_BITS - 1);
  668.       return ((exponent == -1) || (! exponent && *p));
  669.     }
  670.   else if (len == 8)
  671.     {
  672.       exponent = *(p+1);
  673.       exponent = exponent << 1 >> (32 - DOUBLE_EXP_BITS - 1);
  674.       return ((exponent == -1) || (! exponent && *p * *(p+1)));
  675.     }
  676.   else return 1;
  677. }
  678.  
  679. /*
  680.  * Implemented for Irix 4.x by Garrett A. Wollman
  681.  */
  682. #ifdef USE_PROC_FS        /* Target-dependent /proc support */
  683.  
  684. #include <sys/time.h>
  685. #include <sys/procfs.h>
  686.  
  687. typedef unsigned int greg_t;    /* why isn't this defined? */
  688.  
  689. /*
  690.  * See the comment in m68k-tdep.c regarding the utility of these functions.
  691.  */
  692.  
  693. void 
  694. supply_gregset (gregsetp)
  695.      gregset_t *gregsetp;
  696. {
  697.   register int regno;
  698.   register greg_t *regp = (greg_t *)(gregsetp->gp_regs);
  699.  
  700.   /* FIXME: somewhere, there should be a #define for the meaning
  701.      of this magic number 32; we should use that. */
  702.   for(regno = 0; regno < 32; regno++)
  703.     supply_register (regno, (char *)(regp + regno));
  704.  
  705.   supply_register (PC_REGNUM, (char *)&(gregsetp->gp_pc));
  706.   supply_register (HI_REGNUM, (char *)&(gregsetp->gp_mdhi));
  707.   supply_register (LO_REGNUM, (char *)&(gregsetp->gp_mdlo));
  708.   supply_register (PS_REGNUM, (char *)&(gregsetp->gp_cause));
  709. }
  710.  
  711. void
  712. fill_gregset (gregsetp, regno)
  713.      gregset_t *gregsetp;
  714.      int regno;
  715. {
  716.   int regi;
  717.   register greg_t *regp = (greg_t *)(gregsetp->gp_regs);
  718.   extern char registers[];
  719.  
  720.   /* same FIXME as above wrt 32*/
  721.   for (regi = 0; regi < 32; regi++)
  722.     if ((regno == -1) || (regno == regi))
  723.       *(regp + regno) = *(greg_t *) ®isters[REGISTER_BYTE (regi)];
  724.  
  725.   if ((regno == -1) || (regno == PC_REGNUM))
  726.     gregsetp->gp_pc = *(greg_t *) ®isters[REGISTER_BYTE (PC_REGNUM)];
  727.  
  728.   if ((regno == -1) || (regno == PS_REGNUM))
  729.     gregsetp->gp_cause = *(greg_t *) ®isters[REGISTER_BYTE (PS_REGNUM)];
  730.  
  731.   if ((regno == -1) || (regno == HI_REGNUM))
  732.     gregsetp->gp_mdhi = *(greg_t *) ®isters[REGISTER_BYTE (HI_REGNUM)];
  733.  
  734.   if ((regno == -1) || (regno == LO_REGNUM))
  735.     gregsetp->gp_mdlo = *(greg_t *) ®isters[REGISTER_BYTE (LO_REGNUM)];
  736. }
  737.  
  738. /*
  739.  * Now we do the same thing for floating-point registers.
  740.  * We don't bother to condition on FP0_REGNUM since any
  741.  * reasonable MIPS configuration has an R3010 in it.
  742.  *
  743.  * Again, see the comments in m68k-tdep.c.
  744.  */
  745.  
  746. void
  747. supply_fpregset (fpregsetp)
  748.      fpregset_t *fpregsetp;
  749. {
  750.   register int regno;
  751.  
  752.   for (regno = 0; regno < 32; regno++)
  753.     supply_register (FP0_REGNUM + regno,
  754.              (char *)&fpregsetp->fp_r.fp_regs[regno]);
  755.  
  756.   supply_register (FCRCS_REGNUM, (char *)&fpregsetp->fp_csr);
  757.  
  758.   /* FIXME: how can we supply FCRIR_REGNUM?  SGI doesn't tell us. */
  759. }
  760.  
  761. void
  762. fill_fpregset (fpregsetp, regno)
  763.      fpregset_t *fpregsetp;
  764.      int regno;
  765. {
  766.   int regi;
  767.   char *from, *to;
  768.   extern char registers[];
  769.  
  770.   for (regi = FP0_REGNUM; regi < FP0_REGNUM + 32; regi++)
  771.     {
  772.       if ((regno == -1) || (regno == regi))
  773.     {
  774.       from = (char *) ®isters[REGISTER_BYTE (regi)];
  775.       to = (char *) &(fpregsetp->fp_r.fp_regs[regi]);
  776.       bcopy(from, to, REGISTER_RAW_SIZE (regno));
  777.     }
  778.     }
  779.  
  780.   if ((regno == -1) || (regno == FCRCS_REGNUM))
  781.     fpregsetp->fp_csr = *(unsigned *) ®isters[REGISTER_BYTE(FCRCS_REGNUM)];
  782. }
  783.  
  784. #endif /* USE_PROC_FS */
  785.  
  786. /* To skip prologues, I use this predicate. Returns either PC
  787.    itself if the code at PC does not look like a function prologue,
  788.    PC+4 if it does (our caller does not need anything more fancy). */
  789.  
  790. CORE_ADDR
  791. mips_skip_prologue(pc)
  792.      CORE_ADDR pc;
  793. {
  794.     struct symbol *f;
  795.     struct block *b;
  796.     unsigned long inst;
  797.     int offset;
  798.  
  799.     /* For -g modules and most functions anyways the
  800.        first instruction adjusts the stack.
  801.        But we allow some number of stores before the stack adjustment.
  802.        (These are emitted by varags functions compiled by gcc-2.0. */
  803.     for (offset = 0; offset < 100; offset += 4) {
  804.     inst = read_memory_integer(pc + offset, 4);
  805.     if ((inst & 0xffff0000) == 0x27bd0000) /* addiu $sp,$sp,offset */
  806.         return pc + offset + 4;
  807.     if ((inst & 0xFFE00000) != 0xAFA00000) /* sw reg,n($sp) */
  808.         break;
  809.     }
  810.  
  811.     /* Well, it looks like a frameless. Let's make sure.
  812.        Note that we are not called on the current PC,
  813.        but on the function`s start PC, and I have definitely
  814.        seen optimized code that adjusts the SP quite later */
  815.     b = block_for_pc(pc);
  816.     if (!b) return pc;
  817.  
  818.     f = lookup_symbol(".gdbinfo.", b, LABEL_NAMESPACE, 0, NULL);
  819.     if (!f) return pc;
  820.     /* Ideally, I would like to use the adjusted info
  821.        from mips_frame_info(), but for all practical
  822.        purposes it will not matter (and it would require
  823.        a different definition of SKIP_PROLOGUE())
  824.  
  825.        Actually, it would not hurt to skip the storing
  826.        of arguments on the stack as well. */
  827.     if (((mips_extra_func_info_t)SYMBOL_VALUE(f))->pdr.frameoffset)
  828.     return pc + 4;
  829.  
  830.     return pc;
  831. }
  832.  
  833. /* Figure out where the longjmp will land.
  834.    We expect the first arg to be a pointer to the jmp_buf structure from which
  835.    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
  836.    This routine returns true on success. */
  837.  
  838. int
  839. get_longjmp_target(pc)
  840.      CORE_ADDR *pc;
  841. {
  842.   CORE_ADDR jb_addr;
  843.  
  844.   jb_addr = read_register(A0_REGNUM);
  845.  
  846.   if (target_read_memory(jb_addr + JB_PC * JB_ELEMENT_SIZE, pc,
  847.              sizeof(CORE_ADDR)))
  848.     return 0;
  849.  
  850.   SWAP_TARGET_AND_HOST(pc, sizeof(CORE_ADDR));
  851.  
  852.   return 1;
  853. }
  854.